namespace CIS.Project
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class ProjectInfoEditPage : Page
    {
        protected DropDownList DifficultLevelDdl;
        protected DropDownList EffectDayDdl;
        protected DropDownList EffectMonthDdl;
        protected TextBox effectYearTxb;
        protected Image iBtnProject;
        protected Label lbAmountUnit;
        protected Label lbDifficultLevel;
        protected Label lbEffectD;
        protected Label lbEffectDate;
        protected Label lbEffectM;
        protected Label lbEffectY;
        protected Label LbOther;
        protected Label lbProjectName;
        protected Label lbResponsiblePerson;
        protected Label lbSignedD;
        protected Label lbSignedDate;
        protected Label lbSignedM;
        protected Label lbSignedY;
        protected Label lbTotalAmount;
        protected TextBox OtherTxb;
        protected UserPrincipal Principal = null;
        protected TextBox ProjectNameTxb;
        protected TextBox ResponsiblePersonTxb;
        protected Button SaveBtn;
        protected DropDownList SignedDayDdl;
        protected DropDownList SignedMonthDdl;
        protected DropDownList SignedYearDdl;
        protected TextBox TotalAmountTxb;

        private void InitializeComponent()
        {
            this.SaveBtn.Click += new EventHandler(this.SaveBtn_Click);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.SetFrom();
            this.TotalAmountTxb.Text = "0.00";
            this.DifficultLevelDdl.Items.Clear();
            string stri = "";
            for (int i = 1; i <= 5; i++)
            {
                ListItem item = new ListItem();
                item.Value = i.ToString();
                stri = stri + "*";
                item.Text = stri;
                this.DifficultLevelDdl.Items.Add(item);
            }
            for (int i = DateTime.Now.Year; i >= 0x78a; i--)
            {
                ListItem YearItem = new ListItem();
                YearItem.Text = i.ToString();
                this.SignedYearDdl.Items.Add(YearItem);
            }
            for (int i = 1; i <= 12; i++)
            {
                ListItem MonthItem = new ListItem();
                MonthItem.Text = i.ToString();
                this.SignedMonthDdl.Items.Add(MonthItem);
            }
            for (int i = 1; i <= 0x1f; i++)
            {
                ListItem DateItem = new ListItem();
                DateItem.Text = i.ToString();
                this.SignedDayDdl.Items.Add(DateItem);
            }
            this.effectYearTxb.Text = this.SignedYearDdl.SelectedItem.Text;
            for (int i = 1; i <= 12; i++)
            {
                ListItem MonthItem = new ListItem();
                MonthItem.Text = i.ToString();
                this.EffectMonthDdl.Items.Add(MonthItem);
            }
            for (int i = 1; i <= 0x1f; i++)
            {
                ListItem DateItem = new ListItem();
                DateItem.Text = i.ToString();
                this.EffectDayDdl.Items.Add(DateItem);
            }
            if (base.Request.QueryString["IsModify"] == "True")
            {
                Guid id = new Guid(base.Request.QueryString["ProjectId"]);
                Project project = ManageProject.GetProjectInfoViaId(id);
                this.ProjectNameTxb.Text = project.ProjectName;
                this.TotalAmountTxb.Text = project.TotalAmount.ToString();
                this.ResponsiblePersonTxb.Text = project.ResponsiblePerson;
                this.OtherTxb.Text = project.Other;
                for (int i = 0; i < this.SignedYearDdl.Items.Count; i++)
                {
                    if (this.SignedYearDdl.Items[i].Text == project.SignedDate.Year.ToString())
                    {
                        this.SignedYearDdl.SelectedIndex = i;
                        break;
                    }
                }
                for (int i = 0; i < this.SignedMonthDdl.Items.Count; i++)
                {
                    if (this.SignedMonthDdl.Items[i].Text == project.SignedDate.Month.ToString())
                    {
                        this.SignedMonthDdl.SelectedIndex = i;
                        break;
                    }
                }
                for (int i = 0; i < this.SignedDayDdl.Items.Count; i++)
                {
                    if (this.SignedDayDdl.Items[i].Text == project.SignedDate.Day.ToString())
                    {
                        this.SignedDayDdl.SelectedIndex = i;
                        break;
                    }
                }
                this.effectYearTxb.Text = project.EffectDate.Year.ToString();
                for (int i = 0; i < this.EffectMonthDdl.Items.Count; i++)
                {
                    if (this.EffectMonthDdl.Items[i].Text == project.EffectDate.Month.ToString())
                    {
                        this.EffectMonthDdl.SelectedIndex = i;
                        break;
                    }
                }
                for (int i = 0; i < this.EffectDayDdl.Items.Count; i++)
                {
                    if (this.EffectDayDdl.Items[i].Text == project.EffectDate.Day.ToString())
                    {
                        this.EffectDayDdl.SelectedIndex = i;
                        break;
                    }
                }
                for (int i = 0; i < this.DifficultLevelDdl.Items.Count; i++)
                {
                    if (this.DifficultLevelDdl.Items[i].Value == project.DifficultLevel)
                    {
                        this.DifficultLevelDdl.SelectedIndex = i;
                        break;
                    }
                }
            }
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.SelectAuth();
            }
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            string strProjectName = this.ProjectNameTxb.Text.Trim();
            string strTotalAmount = this.TotalAmountTxb.Text.Trim();
            string strResponsiblePerson = this.ResponsiblePersonTxb.Text.Trim();
            string strOther = this.OtherTxb.Text.Trim();
            DateTime SignedDate = new DateTime(Convert.ToInt32(this.SignedYearDdl.SelectedItem.Text), Convert.ToInt32(this.SignedMonthDdl.SelectedItem.Text), Convert.ToInt32(this.SignedDayDdl.SelectedItem.Text));
            DateTime EffectDate = new DateTime(Convert.ToInt32(this.effectYearTxb.Text.Trim()), Convert.ToInt32(this.EffectMonthDdl.SelectedItem.Text), Convert.ToInt32(this.EffectDayDdl.SelectedItem.Text));
            if (base.Request.QueryString["IsModify"] == "True")
            {
                if (strProjectName != string.Empty)
                {
                    ManageProject.UpdateProjectInfo(new Guid(base.Request.QueryString["ProjectId"]), new Guid(base.Request.QueryString["CompanyId"]), strProjectName, Convert.ToSingle(strTotalAmount), SignedDate, EffectDate, this.DifficultLevelDdl.SelectedItem.Value, strResponsiblePerson, strOther);
                    base.Response.Redirect("ProjectInfoPage.aspx?CompanyId=" + base.Request.QueryString["CompanyId"].ToString(), true);
                }
            }
            else if (strProjectName != string.Empty)
            {
                if (!ManageProject.IsExistSameProject(strProjectName))
                {
                    ManageProject.InsertNewProjectInfo(new Guid(base.Request.QueryString["CompanyId"]), strProjectName, Convert.ToSingle(strTotalAmount), SignedDate, EffectDate, this.DifficultLevelDdl.SelectedItem.Value, strResponsiblePerson, strOther);
                    base.Response.Redirect("ProjectInfoPage.aspx?CompanyId=" + base.Request.QueryString["CompanyId"].ToString(), true);
                }
                else
                {
                    base.Response.Write("<script> alert('该信息已存在') </script>");
                }
            }
        }

        private void SelectAuth()
        {
            if (this.Principal.OwnRole.TypeB == 0)
            {
                this.InitPage();
                this.SaveBtn.Visible = false;
                this.ProjectNameTxb.ReadOnly = true;
                this.TotalAmountTxb.ReadOnly = true;
                this.ResponsiblePersonTxb.ReadOnly = true;
                this.OtherTxb.ReadOnly = true;
            }
            else if (this.Principal.OwnRole.TypeB == 1)
            {
                this.InitPage();
            }
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/CIS/LanguageConfig.xml");
            this.lbSignedD.Text = ManageSystemConfig.GetSystemSetLanguage("ProjectInfoEditPage", "lbSignedD", language, path);
            this.lbSignedM.Text = ManageSystemConfig.GetSystemSetLanguage("ProjectInfoEditPage", "lbSignedM", language, path);
            this.lbSignedY.Text = ManageSystemConfig.GetSystemSetLanguage("ProjectInfoEditPage", "lbSignedY", language, path);
            this.lbEffectY.Text = ManageSystemConfig.GetSystemSetLanguage("ProjectInfoEditPage", "lbEffectY", language, path);
            this.lbEffectM.Text = ManageSystemConfig.GetSystemSetLanguage("ProjectInfoEditPage", "lbEffectM", language, path);
            this.lbEffectD.Text = ManageSystemConfig.GetSystemSetLanguage("ProjectInfoEditPage", "lbEffectD", language, path);
            this.LbOther.Text = ManageSystemConfig.GetSystemSetLanguage("ProjectInfoEditPage", "LbOther", language, path);
            this.lbSignedDate.Text = ManageSystemConfig.GetSystemSetLanguage("ProjectInfoEditPage", "lbSignedDate", language, path);
            this.lbEffectDate.Text = ManageSystemConfig.GetSystemSetLanguage("ProjectInfoEditPage", "lbEffectDate", language, path);
            this.lbDifficultLevel.Text = ManageSystemConfig.GetSystemSetLanguage("ProjectInfoEditPage", "lbDifficultLevel", language, path);
            this.lbTotalAmount.Text = ManageSystemConfig.GetSystemSetLanguage("ProjectInfoEditPage", "lbTotalAmount", language, path);
            this.lbProjectName.Text = ManageSystemConfig.GetSystemSetLanguage("ProjectInfoEditPage", "lbProjectName", language, path);
            this.lbResponsiblePerson.Text = ManageSystemConfig.GetSystemSetLanguage("ProjectInfoEditPage", "lbResponsiblePerson", language, path);
            this.SaveBtn.Text = ManageSystemConfig.GetSystemSetLanguage("ProjectInfoEditPage", "SaveBtn", language, path);
            this.lbAmountUnit.Text = ManageSystemConfig.GetSystemSetLanguage("ProjectInfoEditPage", "lbAmountUnit", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iBtnProject.ImageUrl = "../images/title_ProjectInfo.jpg";
                    break;

                case "en":
                    this.iBtnProject.ImageUrl = "../images/title_ProjectInfoEn.jpg";
                    break;
            }
        }
    }
}

